class ListNode {
    val: number;
    next: ListNode | null;
    constructor(val?: number, next?: ListNode | null) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
    }
}
function hasCycle(head: ListNode | null): boolean {

    let slow: ListNode | null = head;
    let fast: ListNode | null = head;

    while (slow && fast && fast.next) {
        slow = slow.next;        // 慢指针走一步
        fast = fast.next.next;    // 快指针走两步

        // 如果快慢指针相遇，说明有环
        if (slow === fast) {
            return true;
        }
    }

    // 如果快指针到达链表末尾，说明无环
    return false;
};